//
//  HeatMapViewCell.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/4.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "HeatMapViewCell.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

@interface HeatMapViewCell()<MAMapViewDelegate>
@property (nonatomic,strong)MAMapView *mapView;
@property (nonatomic,strong)UILabel *titleLabel;
@end

@implementation HeatMapViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
    _titleLabel = [UILabel LabelWithFont:TitleLagerFontSize andColor:TitleColor andTextAlignment:left andString:@"车辆分布图"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(33);
        make.centerY.mas_equalTo(self.mas_top).offset(11+29*0.5);
    }];
    
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 40,kScreenWidth, self.bounds.size.height-40)];
    _mapView.showsScale = NO;
    _mapView.showsCompass = NO;
    _mapView.zoomLevel = 3;
    _mapView.rotateEnabled = NO;
    _mapView.rotateCameraEnabled = NO;
    _mapView.delegate = self;
    MACoordinateSpan span = MACoordinateSpanMake(50, 62);
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(28, 104);
    MACoordinateRegion regoin = MACoordinateRegionMake(coordinate, span);
    _mapView.region = regoin;
    [self addSubview:_mapView];
}

- (void)loadMapWithArr:(NSArray *)arr
{
    
   
    MAHeatMapTileOverlay  *heatMapTileOverlay = [[MAHeatMapTileOverlay alloc] init];
    //构造热力图数据，从locations.json中读取经纬度
    NSMutableArray* data = [NSMutableArray array];

    
    @autoreleasepool {
        
        if (arr)
        {
            for (NSDictionary *tempDict in arr){
                NSArray *dicArray = tempDict[@"point"];
                for (NSArray *point in dicArray)
                {
                    MAHeatMapNode *node = [[MAHeatMapNode alloc] init];
                    CLLocationCoordinate2D coordinate;
                    coordinate.latitude = [[point lastObject] doubleValue];
                    coordinate.longitude = [[point firstObject] doubleValue];
                    node.coordinate = coordinate;
                    node.intensity = 1;//设置权重
                    [data addObject:node];
                }
            }
        }
    }
    
    if (data.count == 0) {
        return;
    }
    
        heatMapTileOverlay.data = data;
        //构造渐变色对象
        MAHeatMapGradient *gradient = [[MAHeatMapGradient alloc] initWithColor:@[[UIColor greenColor], [UIColor redColor]] andWithStartPoints:@[@(0.1),@(0.9)]];
        heatMapTileOverlay.gradient = gradient;
        //将热力图添加到地图上
        [_mapView addOverlay: heatMapTileOverlay];
}
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MATileOverlay class]])
    {
        MATileOverlayRenderer *tileOverlayRenderer = [[MATileOverlayRenderer alloc] initWithTileOverlay:overlay];
        return tileOverlayRenderer;
    }
    return nil;
}

@end
